home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Sprocket / •Source / TEnemySprite.cp < prev    next >
Encoding:
Text File  |  1997-11-05  |  3.6 KB  |  139 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2.  
  3. TEnemySprite.cp
  4.  
  5. This is the class used to represent the enemy ship in the game.  Eventually,
  6. one ship class will represent both friendly and enemy ships, since the only
  7. major difference is the keyset.  A computer controlled ship could be 
  8. different.
  9.  
  10.  
  11. Author: Timothy Carroll
  12. Apple Developer Technical Support
  13. timc@apple.com
  14.  
  15. Modification History: 
  16.  
  17. 1/23/97     TMC        Added include for Moofwars.h so that MrC will compile
  18. 8/15/96        TMC     Initial Release
  19.  
  20. Copyright © 1996, 1997 Apple Computer, Inc., All Rights Reserved
  21.  
  22.  
  23. You may incorporate this sample code into your applications without
  24. restriction, though the sample code has been provided "AS IS" and the
  25. responsibility for its operation is 100% yours.  However, what you are
  26. not permitted to do is to redistribute the source as "DSC Sample Code"
  27. after having made changes. If you're going to re-distribute the source,
  28. we require that you make it clear in the source that the code was
  29. descended from Apple Sample Code, but that you've made changes.
  30.  
  31. *************************************************************************************/
  32.  
  33. #include "Moofwars.h"
  34. #include "TEnemySprite.h"
  35. #include "TShotSprite.h"
  36.  
  37.  
  38.  
  39. TEnemySprite::TEnemySprite (TEnemySpriteData *data):
  40. TSprite((TSpriteData *) data)
  41. {
  42.     fRotateInterval = data->rotateInterval;
  43.     fRotateValue = 0;
  44.     fShotInterval = data->shotInterval;
  45.     fShotValue = 0;
  46.     fShotsGroup = data->shotsGroup;
  47. }
  48.  
  49.  
  50. TEnemySprite::~TEnemySprite (void)
  51. {
  52. }
  53.     
  54. void
  55. TEnemySprite::ProcessSprite ()
  56. {    
  57.     // The enemy can thrust in either direction
  58.     if (CheckKey ((unsigned char *) &gCommandKeys, kEnemyThrustForward))
  59.     {
  60.         fVelocityY -= gSinLookup [fFace] >> 1;
  61.         fVelocityX -= gCosLookup [fFace] >> 1;
  62.     }
  63.             
  64.     if (CheckKey ((unsigned char *) &gCommandKeys, kEnemyThrustBackward))
  65.     {
  66.         fVelocityY += gSinLookup [fFace] >> 1;
  67.         fVelocityX += gCosLookup [fFace] >> 1;
  68.     }
  69.  
  70.     // Fire a shot
  71.     if (CheckKey ((unsigned char *) &gCommandKeys, kEnemyFireShots) && fShotValue == 0)
  72.     {
  73.         TShotSpriteData     shotData;
  74.         TShotSprite            *shot;
  75.         int                    index;
  76.         
  77.         // Delay for next set of shots
  78.         fShotValue = fShotInterval;
  79.         
  80.         shotData.spriteData.spriteType = TShotSprite::kSpriteType;
  81.         shotData.spriteData.visibility = kVisible;
  82.         shotData.spriteData.face = 1;
  83.         shotData.spriteData.collectionID = 1001;
  84.         shotData.spriteData.preloadedCollection = NULL;
  85.         shotData.duration = 60;
  86.     
  87.         index = fFace;
  88.         
  89.         shotData.spriteData.initialX = fCoordX - 60*gCosLookup [index];
  90.         shotData.spriteData.initialY = fCoordY - 60*gSinLookup [index];
  91.         shotData.spriteData.initialXVelocity = fVelocityX - 30*gCosLookup[index];
  92.         shotData.spriteData.initialYVelocity = fVelocityY - 30*gSinLookup[index];
  93.         shot = new TShotSprite (&shotData);
  94.         shot->AddToGroup (fShotsGroup);
  95.  
  96.     }
  97.  
  98.     
  99.     // Rotate right
  100.     if (CheckKey ((unsigned char *) &gCommandKeys, kEnemyRotateRight) && fRotateValue == 0)
  101.     {
  102.         fFace = (fFace+1) % 48;
  103.         fRotateValue = fRotateInterval;
  104.     }
  105.  
  106.     
  107.     // Rotate left
  108.     if (CheckKey ((unsigned char *) &gCommandKeys, kEnemyRotateLeft) && fRotateValue == 0)
  109.     {
  110.         fFace = (fFace+47) % 48;
  111.         fRotateValue = fRotateInterval;
  112.     }
  113.     
  114.     
  115.     // Adjust our intervals
  116.     if (fRotateValue > 0)
  117.         fRotateValue--;
  118.         
  119.     if (fShotValue > 0)
  120.         fShotValue--;
  121.         
  122.     TSprite::ProcessSprite();
  123.     TSprite::Bounce (&gWorldBounds);
  124. }
  125.  
  126.  
  127. void
  128. TEnemySprite::Collision (TSprite *theSprite)
  129. {
  130. #if qUsingSound
  131.     if (gPlayingSound)
  132.         HY_PlaySoundHandle (2, gSounds[3], NULL, 0, false);
  133. #endif        
  134.     // We take on some of the velocity of the shot.
  135.     TShotSprite *theTarget = (TShotSprite *) theSprite;
  136.     
  137.     //this->fCurVelocityX += theTarget->GetXVelocity() >> 9;
  138.     //this->fCurVelocityY += theTarget->GetYVelocity() >> 9;
  139. }